package com.gzzz.output;

import java.io.*;
import java.util.ArrayList;
import java.util.Properties;

public class FileOutput {

    /**
     * 将传入的ArrayList<String>写入指定的File
     * @param list 要写入文件的ArrayList
     * @par
     */
    public static void  writeListToFile(ArrayList<String> list,String filePath){
        try {
            if (filePath == null) {
                InputStream is = FileOutput.class.getClassLoader().getResourceAsStream("IO.properties");
                Properties properties = new Properties();
                properties.load(is);
                filePath = properties.getProperty("outputPath")+"\\"+properties.getProperty("fileName");
            }

            // 创建一个File对象，表示要将列表写入的文件
            File outputFile = new File(filePath);
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));

            // 遍历ArrayList，将每个元素写入文件
            if (list != null) {
                for (String item : list) {
                    writer.write(item);
                    writer.newLine(); // 在每个元素后添加换行符
                }
            }
            writer.flush();
        } catch (IOException e) {
            System.err.println("Error writing to file: " + e.getMessage());
        }
    }

}
